package cn.javabb.admin.controller.biz;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.query.BlogQuery;
import cn.javabb.core.service.blog.BlogService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.StateAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/05 11:17
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController extends BaseController {

    @Autowired
    private BlogService blogService;
    /**
     * 列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
       // List<Tree> catalogTree = catalogService.listTree();
        //model.addAttribute("catalogTree", catalogTree);
        return "admin/biz/blog/blog";
    }
    /**
     * 保存页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/biz/blog/blogSave";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("blog:view")
    @ResponseBody
    @GetMapping("/")
    public Object list(BlogQuery query) {
        PageInfo p = blogService.queryPage(query);
        return ResponseModel.table(p);
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("blog:del")
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "博客删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if (blogService.delete(ids)) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    @RequiresPermissions("blog:edit")
    @ResponseBody
    @PostMapping("/updateState")
    @Log(name="博客状态",action = StateAction.class)
    public Object updateState(String ids,Integer state){
        if(blogService.updateState(ids,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
    /**
     * 更新状态
     * @param ids
     * @param blogTop
     * @return
     */
    @RequiresPermissions("blog:edit")
    @ResponseBody
    @PostMapping("/updateTop")
    @Log(name="博客状态",action = StateAction.class)
    public Object updateTop(String ids,Boolean blogTop){
        if(blogService.updateTop(ids,blogTop)){
            return ResponseModel.Success("置顶成功");
        }
        return ResponseModel.Failure("置顶失败");
    }
    /**
     * 更新状态
     * @param ids
     * @param blogSupport
     * @return
     */
    @RequiresPermissions("blog:edit")
    @ResponseBody
    @PostMapping("/updateSupport")
    @Log(name="博客状态",action = StateAction.class)
    public Object updateSupport(String ids,Boolean blogSupport){
        if(blogService.updateSupport(ids,blogSupport)){
            return ResponseModel.Success("推荐成功");
        }
        return ResponseModel.Failure("推荐失败");
    }
}
